/*! 
 * WeX5 v3 (http://www.justep.com) 
 * Copyright 2015 Justep, Inc.
 * Licensed under Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) 
 */
define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var url = require.normalizeName("./wxRefund");
	var ComponentConfig = require("./wxRefund.config");
	var Expr = justep.Express;

	var wxRefund = justep.BindComponent.extend({

		getConfig: function() {
			return ComponentConfig;
		},
		// 构造函数
		constructor: function(model, xid) {
			this.callParent();
			this.refundType = "";
			this.outTradeNoExpr = "";
			this.outRefundNoExpr = "";
			this.totalFeeExpr = "";
			this.refundFeeExpr = "";
			this.appidExpr = ""
		},
		getAppPath: function() {
			var cfg = this.getConfig();
			return this.appPath || cfg.appPath;
		},

		// 退款前事件
		_onRefundBefore: function() {
			this.fireEvent('onRefundBefore', {
				source: this
			});
		},

		// 退款成功事件
		_onRefundSuccess: function(res) {
			this.fireEvent('onRefundSuccess', {
				source: this,
				data: res
			});
		},

		// 退款失败事件
		_onRefundFail: function() {
			this.fireEvent('onRefundFail', {
				source: this
			});
		},
		// 发起退款 
		sendRefund: function() {
			var self = this;
			var options = {};
			var ctx = justep.Bind.contextFor(this.domNode);
			var url = "";
			// 退款前事件	
			this._onRefundBefore()

			if(!this.refundType){
				justep.Util.hint('请在退款组件属性上选择退款类型！', {
					'type': 'warning'
				});
			} else if(this.refundType == "mp"){
				url = "/wxapi/mp/pay/refund";
			} else if(this.refundType == "miniapp"){
				url = "/wxapi/miniapp/pay/refund";
			}

			if (this.outTradeNoExpr) {
				if (!(this._outTradeNoExpr instanceof Expr)) this._outTradeNoExpr = new Expr(this.outTradeNoExpr);
				options.outTradeNo = Expr.eval(this._outTradeNoExpr, ctx.$object, ctx);
				if(options.outTradeNo == undefined){
					justep.Util.hint('商户订单号不能为空！', {
						'type': 'warning'
					});
					return;
				}
			} else {
				justep.Util.hint('请在退款组件属性上设置商户订单号！', {
					'type': 'warning'
				});
				return;
			}

			if (this.outRefundNoExpr) {
				if (!(this._outRefundNoExpr instanceof Expr)) this._outRefundNoExpr = new Expr(this.outRefundNoExpr);
				options.outRefundNo = Expr.eval(this._outRefundNoExpr, ctx.$object, ctx);
				if(options.outRefundNo == undefined){
					justep.Util.hint('商户退款单号不能为空！', {
						'type': 'warning'
					});
					return;
				}
			} else {
				justep.Util.hint('请在退款组件属性上设置商户退款单号！', {
					'type': 'warning'
				});
				return;
			}

			if (this.totalFeeExpr) {
				if (!(this._totalFeeExpr instanceof Expr)) this._totalFeeExpr = new Expr(this.totalFeeExpr);
				options.totalFee = Expr.eval(this._totalFeeExpr, ctx.$object, ctx);
				options.totalFee = parseFloat(options.totalFee) * 100; //微信接口最小单位为分
				if(options.totalFee == undefined){
					justep.Util.hint('订单金额不能为空！', {
						'type': 'warning'
					});
					return;
				}
			} else {
				justep.Util.hint('请在退款组件属性上设置订单金额！', {
					'type': 'warning'
				});
				return;
			}

			if (this.refundFeeExpr) {
				if (!(this._refundFeeExpr instanceof Expr)) this._refundFeeExpr = new Expr(this.refundFeeExpr);
				options.refundFee = Expr.eval(this._refundFeeExpr, ctx.$object, ctx);
				options.refundFee = parseFloat(options.refundFee) * 100; //微信接口最小单位为分
				if(options.refundFee == undefined){
					justep.Util.hint('退款金额不能为空！', {
						'type': 'warning'
					});
					return;
				}
			} else {
				justep.Util.hint('请在退款组件属性上设置退款金额！', {
					'type': 'warning'
				});
				return;
			}

			if (this.appidExpr && this.refundType=="miniapp") {
				if (!(this._appidExpr instanceof Expr)) this._appidExpr = new Expr(this.appidExpr);
				options.appid = Expr.eval(this._appidExpr, ctx.$object, ctx);
			}

			// 退款	
			$.ajax({
				type: "POST",
				url: require.toUrl(url),
				contentType: 'application/json;charset=UTF-8',
				dataType: 'json',
				async: false,
				cache: false,
				data: JSON.stringify(options),
				success: function(res) {
					var msg = res.msg;
		            if(msg.indexOf('<err_code_des>')>0){
		                msg = msg.split('<err_code_des><![CDATA[')[1].split(']]></err_code_des>')[0]
		            }

					if (res.success && res.data != undefined) {
						if (res.data.resultCode == "SUCCESS") {
							justep.Util.hint("退款成功！", {
								"type": "success"
							});
							// 退款成功事件
							self._onRefundSuccess(res.data);
						} else {
							justep.Util.hint(res.data.errCodeDes, {
								"type": "danger"
							});
							// 退款失败事件
							self._onRefundFail(res.data)
						}
					} else {
						justep.Util.hint(msg, {
							"type": "danger"
						});
						// 退款失败事件
						self._onRefundFail()
					}
				},
				error: function() {
					justep.Util.hint("退款失败！", {
						"type": "danger"
					});
					// 退款失败事件
					self._onRefundFail()
				}
			});
		}
	});

	justep.Component.addOperations(wxRefund, {
		'sendRefund': {
			label: '发起退款',
			argsDef: [],
			method: function() {
				return this.owner.sendRefund();
			}
		}
	});

	justep.Component.register(url, wxRefund);
	return wxRefund;
});